/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpDetails;
import com.jims.master.modules.csm.entity.ExpExportDetail;
import com.jims.master.modules.csm.entity.ExpImportDetail;
import com.jims.master.modules.csm.entity.ExpInfo;
import com.jims.master.modules.csm.service.ExpDetailsService;
import com.jims.master.modules.csm.service.ExpExportService;
import com.jims.master.modules.csm.service.ExpImportDetailService;
import com.jims.master.modules.csm.service.ExpInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消耗理信息Controller
 * @author DT
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expDetails")
public class ExpDetailsController extends BaseController {

	@Autowired
	private ExpDetailsService expDetailsService;
	@Autowired
	private ExpExportService expExportService;
	@Autowired
	private ExpInfoService expInfoService;
	@Autowired
	private ExpImportDetailService expImportDetailService;

	@ModelAttribute
	public ExpDetails get(@RequestParam(required=false) String id) {
		ExpDetails entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expDetailsService.get(id);
		}
		if (entity == null){
			entity = new ExpDetails();
		}
		return entity;
	}

	/**
	 * 根据masterId查询出库详情(出库单据查询)
	 * @param expExportDetail
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"exportdetail", ""})
	public String listexportdetail(ExpExportDetail expExportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
		List<ExpExportDetail> expExportDetaiList= expExportService.findBymasterId(expExportDetail);
		model.addAttribute("expExportDetaiList", expExportDetaiList);
        model.addAttribute("expExportDetail", expExportDetail);
		return "modules/csm/expExportDetailsIndex";
	}

	/**
	 * 入库单据查询----根据masterId查询入库详情
	 * @param expImportDetail
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"importdetail", ""})
	public String listimportdetail(ExpImportDetail expImportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
		List<ExpImportDetail> expImportDetailList= expImportDetailService.findBymasterId(expImportDetail);
		model.addAttribute("expImportList", expImportDetailList);
        model.addAttribute("expImportDetail", expImportDetail);
		return "modules/csm/expImportDetailsIndex";
	}

	/**
	 * 出库记录查询
	 * @param expExportDetail
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"exportQue", ""})
	public String listExportDetail(ExpExportDetail expExportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
		Page<ExpExportDetail> page= expExportService.findDetailList(new Page<ExpExportDetail>(request, response), expExportDetail);
		model.addAttribute("page", page);
		return "modules/csm/expExportDeatailsIndex";
	}

    /**
     * 入库记录查询
     * @param expImportDetail
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"importQue", ""})
    public String listImportDetail(ExpImportDetail expImportDetail,HttpServletRequest request, HttpServletResponse response,  Model model) {
        Page<ExpImportDetail> page= expImportDetailService.findDetailsList(new Page<ExpImportDetail>(request, response), expImportDetail);
        model.addAttribute("page", page);
        return "modules/csm/expImportDeatailsIndex";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpDetails expDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpDetails> page = expDetailsService.findPage(new Page<ExpDetails>(request, response), expDetails);
        model.addAttribute("page", page);
        return "modules/csm/expDetailsIndex";
    }

    /**
     * 消耗理信息添加并获取消耗品信息
     * @param expDetails
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpDetails expDetails, HttpServletRequest request, HttpServletResponse response, Model model) {
        ExpInfo expInfo=new ExpInfo();
        Page<ExpInfo> page=expInfoService.findPage(new Page<ExpInfo>(request,response),expInfo);
        model.addAttribute("page",page);
        model.addAttribute("expDetails", expDetails);
        return "modules/csm/expDetailsForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ExpDetails expDetails, Model model) {
        StringData data=new StringData();
        int i=expDetailsService.save(expDetails);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpDetails expDetails) {
        StringData data=new StringData();
        int i=expDetailsService.delete(expDetails);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
        return data;
    }

    /**
     * 消耗品入库单据打印
     * @param expImportDetail
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "expImportDetailsPrint")
    @ResponseBody
    public StringData expImportDetailsPrint(ExpImportDetail expImportDetail){
        StringData stringData = new StringData();
        String printPath = expImportDetailService.expImportDetailsPrint(expImportDetail);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
    /**
     * 消耗品出库单据打印
     * @param expExportDetail
     * @return com.jims.common.data.StringData
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "expExportDetailsPrint")
    @ResponseBody
    public StringData expExportDetailsPrint(ExpExportDetail expExportDetail){
        StringData stringData = new StringData();
        String printPath = expExportService.expExportDetailsPrint(expExportDetail);
        stringData.setCode("success");
        stringData.setData(printPath);
        return stringData;
    }
}